//
//  QLMVipInfoVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2018/1/25.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "QLMVipInfoVC.h"
#import "QLMXmDouVC.h"
#import "QLMTequanTabCell.h"
#import "QLMVipInfoTabCell.h"

@interface QLMVipInfoVC ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>{
    UIProgressView *progressView;
    NSTimer *timer;
    NSArray *titleArr;
    NSArray *hjTitleArr;
    NSString *chafloat;
}

@end

@implementation QLMVipInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"会员中心";
    
    titleArr=[NSArray arrayWithObjects:@"客服专线",@"服务人员巡检+斗山检测车检查合计每年2次",@"常用件8.5折，性能件9折",@"一般故障免工时、免里程费，大修按标准8折收费",@"免部分资料，免实地信审，免视频连线",@"现场服务培训",@"本人生日会、老婆生日、重大节日礼品、总经理(春节、中秋)亲自上门拜访、集团董事长亲笔信",@"活动名额",nil];
    hjTitleArr=[NSArray arrayWithObjects:@"客服专线",@"服务人员巡检每年1次",@"常用件9折，性能件9.5折",@"一般故障免工时、免里程费，大修按标准9折收费",@"免部分资料，免实地信审，免视频连线",@"现场服务培训",@"本人生日礼品、新品资料",nil];
    //进度条初始化
    progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(55, 140, kScreen_width-110, 30)];
    progressView.transform = CGAffineTransformMakeScale(1.0f, 7.0f);
    progressView.progressImage=[UIImage imageNamed:@"jdt_2"];
    progressView.trackImage=[UIImage imageNamed:@"jdt_1"];
    progressView.progress=0.00;
    [self.view addSubview:progressView];
    
    UILabel *_myphoneLable = [[UILabel alloc]initWithFrame:CGRectMake(55, 155, kScreen_width-110, 20)];
    _myphoneLable.font = [UIFont systemFontOfSize:14.0f];
    _myphoneLable.textAlignment=NSTextAlignmentCenter;
    _myphoneLable.textColor=[UIColor whiteColor];
    _myphoneLable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_myphoneLable];
    
    _timeLabel.font=[UIFont systemFontOfSize:12];
    _timeLabel.text=[NSString stringWithFormat:@"%@",[self timeWithTimeIntervalString:_vipArrivalTime]];
    
    _vipScrollView.backgroundColor=UIColorFromRGB(0xf5f5f5);
    _vipScrollView.contentSize=CGSizeMake(2*kScreen_width,kScreen_height-225-64);
    _vipScrollView.bounces=NO;
    if (_vipLevel.integerValue==1) {
        _noDataImView.hidden=NO;
    }else{
        _noDataImView.hidden=YES;
    }
    
    _vipScrollView.contentOffset=CGPointMake(0,_vipScrollView.frame.size.height);
    [self changeButtonColor:_recordBtn andOtherBtn:_tequanBtn];
    
    _tequanBanner.hidden=NO;
    _recordBanner.hidden=YES;
    
    UITableView *_tetableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height-225-64) style:UITableViewStylePlain];
    _tetableview.backgroundColor=[UIColor clearColor];
    _tetableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tetableview.delegate=self;
    _tetableview.dataSource=self;
    _tetableview.tag=888;
    _tetableview.tableFooterView=[UIView new];
    [_vipScrollView addSubview:_tetableview];
    
    UITableView *_tableview=[[UITableView alloc] initWithFrame:CGRectMake(kScreen_width, 0, kScreen_width, kScreen_height-225-64) style:UITableViewStylePlain];
    _tableview.backgroundColor=[UIColor whiteColor];
    _tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.tableFooterView=[UIView new];
    [_vipScrollView addSubview:_tableview];
    
    QLMCustomTipView *tipView=[QLMCustomTipView sharedInstance];
    tipView.hidden = NO;
    [_tableview addSubview:tipView];
    
    switch ([_vipLevel integerValue]) {
        case 1:
            [self _setUpdateImageView:@"pthy_icon" withNext:@"zzhy_icon" andVipLev:@"pthy_bj"];
            break;
        case 2:
            [self _setUpdateImageView:@"zzhy_icon" withNext:@"sjhy_icon" andVipLev:@"zzhy_bj"];
            break;
        case 3:
            [self _setUpdateImageView:@"sjhy_icon" withNext:@"hjhy_icon" andVipLev:@"sjhy_bj"];
            break;
        case 4:
            [self _setUpdateImageView:@"hjhy_icon" withNext:@"bjhy_icon" andVipLev:@"hjhy_bj"];
            break;
        case 5:
            [self _setUpdateImageView:@"bjhy_icon" withNext:@"bjhy_icon" andVipLev:@"bjhy_bj"];
            break;
        default:
            break;
    }
    chafloat=[NSString stringWithFormat:@"%.2f",([_integrationNew floatValue]-[_vipPointSatrt floatValue])/([_vipPointEnd floatValue]-[_vipPointSatrt floatValue])];
    _myphoneLable.text = [NSString stringWithFormat:@"距离下一级还差%ld分哦",[_vipPoint integerValue]];
    if (@available(iOS 11.0, *)) {
        _tableview.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    
    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(update) userInfo:nil repeats:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString{
    // 格式化时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy.MM.dd"];
    
    // 毫秒值转化为秒
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/1000.0];
    NSString *dateString = [formatter stringFromDate:date];
    
    NSTimeInterval time=365*24*3600;
    NSDate *nextYear=[date dateByAddingTimeInterval:time];
    NSString *date2String = [formatter stringFromDate:nextYear];
    NSString *str=[NSString stringWithFormat:@"%@—%@",dateString,date2String];
    
    return str;
}

- (void)_setUpdateImageView:(NSString *)preStr withNext:(NSString *)nextStr andVipLev:(NSString *)levelStr{
    _preLevelImView.image=[UIImage imageNamed:preStr];
    _nextImView.image=[UIImage imageNamed:nextStr];
    _vipLevelImV.image=[UIImage imageNamed:levelStr];
}

- (void)update{
    progressView.progress = progressView.progress + 0.01;
    NSString *proStr=[NSString stringWithFormat:@"%.2f",progressView.progress];
    if([proStr isEqualToString:chafloat]){
        [timer invalidate];
        timer=nil;
    }
}

- (IBAction)tequanBtnAction:(UIButton *)sender {
    if (sender.tag==0) {
        _vipScrollView.contentOffset=CGPointMake(0, 0);
//        [self changeButtonColor:_recordBtn andOtherBtn:_tequanBtn];
        
        _tequanBanner.hidden=NO;
        _recordBanner.hidden=YES;
        
        if (_vipLevel.integerValue==1) {
            _noDataImView.hidden=NO;
        }else{
            _noDataImView.hidden=YES;
        }
    }else{
        _vipScrollView.contentOffset=CGPointMake(kScreen_width, 0);
//        [self changeButtonColor:_tequanBtn andOtherBtn:_recordBtn];
        
        _tequanBanner.hidden=YES;
        _recordBanner.hidden=NO;
        
        _noDataImView.hidden=YES;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (![scrollView isKindOfClass:[UITableView class]]) {
        if (scrollView.contentOffset.x==kScreen_width) {
            [self changeButtonColor:_tequanBtn andOtherBtn:_recordBtn];
            _tequanBanner.hidden=YES;
            _recordBanner.hidden=NO;
            _noDataImView.hidden=YES;
        }else if (scrollView.contentOffset.x==0){
            [self changeButtonColor:_recordBtn andOtherBtn:_tequanBtn];
            _tequanBanner.hidden=NO;
            _recordBanner.hidden=YES;
            if (_vipLevel.integerValue==1) {
                _noDataImView.hidden=NO;
            }else{
                _noDataImView.hidden=YES;
            }
        }
    }
}

- (void)changeButtonColor:(UIButton *)btn andOtherBtn:(UIButton *)otherBtn{
    [btn setTitleColor:[UIColor colorWithRed:85/255.0 green:85/255.0 blue:85/255.0 alpha:1] forState:UIControlStateNormal];
    [otherBtn setTitleColor:[UIColor colorWithRed:255/255.0 green:174/255.0 blue:0/255.0 alpha:1] forState:UIControlStateNormal];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark -
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (tableView.tag==888) {
        if (_vipLevel.integerValue==1) {
            return 0.1;
        }
        return 60;
    }
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (tableView.tag==888) {
        UIView *footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 10)];
        footerView.backgroundColor=[UIColor clearColor];
        footerView.userInteractionEnabled=YES;
        if (_vipLevel.integerValue!=1) {
            UIView *aView=[[UIView alloc] initWithFrame:CGRectMake(kScreen_width-30, 10, 10, 50)];
            aView.backgroundColor=[UIColor colorWithRed:228/255.0 green:67/255.0 blue:45/255.0 alpha:1];
            [footerView addSubview:aView];
            
            UILabel *_tipsLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, kScreen_width-30, 50)];
            _tipsLable.text = @"更多特权功能，持续开发中。敬请期待！";
            _tipsLable.textAlignment=NSTextAlignmentCenter;
            _tipsLable.font = [UIFont boldSystemFontOfSize:16.f];
            _tipsLable.textColor=[UIColor colorWithRed:167/255.0 green:167/255.0 blue:167/255.0 alpha:1];
            _tipsLable.backgroundColor = [UIColor whiteColor];
            [footerView addSubview:_tipsLable];
        }
        return footerView;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag==888) {
        if (_vipLevel.integerValue<3) {
            return 0;
        }else if (_vipLevel.integerValue==3){
            return 1;
        }else if (_vipLevel.integerValue==4){
            return hjTitleArr.count;
        }
        return titleArr.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==888) {
        return 50;
    }
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==888) {
        
        QLMTequanTabCell *teCell=[QLMTequanTabCell cellWithTableView:tableView];
        teCell.selectionStyle=UITableViewCellSelectionStyleNone;
        teCell.backgroundColor=[UIColor clearColor];

        if (_vipLevel.integerValue==5) {
            if (indexPath.row!=titleArr.count-1) {
                UIView *aView=[[UIView alloc] initWithFrame:CGRectMake(15, 49, kScreen_width-30-15, 1)];
                aView.backgroundColor=UIColorFromRGB(0xf5f5f5);
                [teCell addSubview:aView];
            }
            teCell.titleLabel.text=[NSString stringWithFormat:@"%ld、%@",indexPath.row+1,titleArr[indexPath.row]];
        }else if (_vipLevel.integerValue==4) {
            if (indexPath.row!=hjTitleArr.count-1) {
                UIView *aView=[[UIView alloc] initWithFrame:CGRectMake(15, 49, kScreen_width-30-15, 1)];
                aView.backgroundColor=UIColorFromRGB(0xf5f5f5);
                [teCell addSubview:aView];
            }
            teCell.titleLabel.text=[NSString stringWithFormat:@"%ld、%@",indexPath.row+1,hjTitleArr[indexPath.row]];
        }else if (_vipLevel.integerValue==3) {
            teCell.titleLabel.text=[NSString stringWithFormat:@"%ld、%@",indexPath.row+1,@"常用件9.5折，性能件9.5折"];
        }
        
        return teCell;
    }
    QLMVipInfoTabCell *vipCell=[QLMVipInfoTabCell cellWithTableView:tableView];
    vipCell.selectionStyle=UITableViewCellSelectionStyleNone;
    return vipCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView.tag!=888) {
        QLMXmDouVC *scanVC=[QLMXmDouVC new];
        self.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:scanVC animated:YES];
    }
}

@end
